package com.loooooo.pm2024.utils;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;

/**
 * 本类用于对我们二维码进行参数的设定,生成我们的二维码：
 *
 * @author kingwen
 */
public class QRCodeFactory {

    public BufferedImage setMatrixLogo(BufferedImage matrixImage, InputStream logoStream) throws IOException {

        if (logoStream == null)
            return matrixImage;

        /**
         * 读取二维码图片，并构建绘图对象
         */
        Graphics2D g2 = matrixImage.createGraphics();
        int matrixWidth = matrixImage.getWidth();
        int matrixHeigh = matrixImage.getHeight();

        /**
         * 读取Logo图片
         */
        BufferedImage logo = ImageIO.read(logoStream);

        // 开始绘制图片
        g2.drawImage(logo, matrixWidth / 5 * 2, matrixHeigh / 5 * 2, matrixWidth / 5, matrixHeigh / 5, null);
        // 绘制边框
        BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        // 设置笔画对象
        g2.setStroke(stroke);
        // 指定弧度的圆角矩形
        RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth / 5 * 2, matrixHeigh / 5 * 2,
                matrixWidth / 5, matrixHeigh / 5, 20, 20);
        g2.setColor(Color.white);
        // 绘制圆弧矩形
        g2.draw(round);

        // 设置logo 有一道灰色边框
        BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        // 设置笔画对象
        g2.setStroke(stroke2);
        RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth / 5 * 2 + 2, matrixHeigh / 5 * 2 + 2,
                matrixWidth / 5 - 4, matrixHeigh / 5 - 4, 20, 20);
        g2.setColor(new Color(128, 128, 128));
        g2.draw(round2);// 绘制圆弧矩形

        g2.dispose();
        matrixImage.flush();
        return matrixImage;

    }


    public void creatQrImage(String content, String format, OutputStream out, InputStream logoStream, Integer width, Integer height)
            throws IOException, WriterException {

        if (width == null) width = 430;
        if (height == null) height = 430;


        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        // 指定纠错等级,纠错级别（L 7%、M 15%、Q 25%、H 30%）
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 内容所使用字符集编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
        // hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值
        // ytx
        // hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度，非负数

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, // 要编码的内容
                // 编码类型，目前zxing支持：Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
                // Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
                // MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E
                // 1D,UPC/EAN extension,UPC_EAN_EXTENSION
                BarcodeFormat.QR_CODE, width, // 条形码的宽度
                height, // 条形码的高度
                hints);// 生成条形码时的一些配置,此项可选
        MatrixToImageWriter.write(bitMatrix, format, out, logoStream);
    }

    public BufferedImage creatQrImage(String content, Integer width, Integer height) throws IOException, WriterException {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        // 指定纠错等级,纠错级别（L 7%、M 15%、Q 25%、H 30%）
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 内容所使用字符集编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 0);
        // hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
        // hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值
        // ytx
        // hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度，非负数
        return creatQrImage(content, width, height, null);
    }

    public BufferedImage creatQrImage(String content, Integer width, Integer height, Hashtable<EncodeHintType, Object> hints)
            throws IOException, WriterException {

        if (width == null) width = 430;
        if (height == null) height = 430;

        if (hints == null) {
            hints = new Hashtable<EncodeHintType, Object>();
            // 指定纠错等级,纠错级别（L 7%、M 15%、Q 25%、H 30%）
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            // 内容所使用字符集编码
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.MARGIN, 0);
            // hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
            // hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值
            // ytx
            // hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度，非负数
        }

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, // 要编码的内容
                // 编码类型，目前zxing支持：Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
                // Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
                // MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E
                // 1D,UPC/EAN extension,UPC_EAN_EXTENSION
                BarcodeFormat.QR_CODE, width, // 条形码的宽度
                height, // 条形码的高度
                hints);// 生成条形码时的一些配置,此项可选
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }
}
